/********************************************
 * 功能说明: Lessee实体对应的业务逻辑Service实现类
 * 模块名称:
 * 系统名称:
 * 软件版权: Frank
 * 系统版本: 1.0.0.1
 * 开发人员:
 * 开发时间: 2020-03-24 10:07:54
 * 审核人员:
 * 相关文档:
 * 修改记录: 修改日期 修改人员 修改说明
 *********************************************/
package com.spring.arch.operation.tenant.service;

import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.repository.LesseeContactRepository;
import com.spring.arch.operation.tenant.repository.LesseeRepository;
import com.spring.arch.common.exception.ExceptionHolder;
import com.spring.arch.operation.tenant.enums.LesseeStatus;
import com.spring.arch.operation.tenant.errorcode.TenantErrorCode;
import com.spring.arch.operation.tenant.model.Lessee;
import com.spring.arch.operation.tenant.model.LesseeContact;
import com.spring.arch.operation.tenant.model.LesseeInvoice;
import com.spring.arch.operation.tenant.repository.LesseeContactRepository;
import com.spring.arch.operation.tenant.repository.LesseeInvoiceRepository;
import com.spring.arch.operation.tenant.repository.LesseeRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;


/**
 * Lessee实体对应的业务逻辑Service实现类
 * @author
 * @version 1.0.0.1
 * @since 2020-03-24 10:07:54
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class LesseeService {

    @Autowired
    private LesseeRepository lesseeRepository;
	@Autowired
	private LesseeContactRepository lesseeContactRepository;
	@Autowired
	private LesseeInvoiceRepository lesseeInvoiceRepository;

	public Lessee save(final Lessee lessee, final List<LesseeContact> contacts, final List<LesseeInvoice> invoices) {
		this.existsLessee(lessee);
		Lessee model = this.lesseeRepository.save(lessee);
		this.lesseeContactRepository.deleteByLesseeId(lessee.getId());
		if(CollectionUtils.isNotEmpty(contacts)) {
			contacts.stream().forEach(contact -> {
				contact.setLessee(model);
				this.lesseeContactRepository.save(contact);
			});
		}

		this.lesseeInvoiceRepository.deleteByLesseeId(lessee.getId());
		if(CollectionUtils.isNotEmpty(invoices)) {
			invoices.stream().forEach(invoice -> {
				invoice.setLessee(model);
				this.lesseeInvoiceRepository.save(invoice);
			});
		}

		return model;
	}

	public void existsLessee(final Lessee lessee) {
		final boolean exists;
		if (lessee.isNew()) {
			exists = this.lesseeRepository.existsByName(lessee.getName());
		} else {
			exists = this.lesseeRepository.existsByNameAndIdNot(lessee.getName(), lessee.getId());
		}
		if (exists) {
			throw ExceptionHolder.serviceException(TenantErrorCode.E1201000002, "租客名称");
		}
	}

	public Lessee save(final Lessee lessee) {
		this.existsLessee(lessee);
		return this.lesseeRepository.save(lessee);
	}

	public void disable(UUID id) {
		this.lesseeRepository.findById(id).ifPresent(model -> {
			model.setStatus(LesseeStatus.HISTORY);
			model.setDisable(true);
			this.lesseeRepository.save(model);
		});
	}
}
